package net.cathackers.devel.scmp.chat.impl.xmpp.iqcomponent.roster;


import org.apache.log4j.Logger;

import net.cathackers.devel.scmp.auth.interfaces.UserID;
import net.cathackers.devel.scmp.chat.exceptions.RosterItemNotFoundException;
import net.cathackers.devel.scmp.chat.interfaces.RosterBackend;
import net.cathackers.devel.scmp.chat.interfaces.RosterSubscriptionManager;
import net.cathackers.devel.scmp.exceptions.BackendException;
import net.cathackers.devel.scmp.factories.SCMPFactory;


public class RosterSubscriptionManagerImpl implements RosterSubscriptionManager {
    private RosterBackend backend_;
    private Logger logger_;

    public RosterSubscriptionManagerImpl() {
	this.backend_ 	= SCMPFactory.getRosterBackend();
	this.logger_  	= SCMPFactory.getLogger(this);
    }

    @Override
    public UserID[] getPublishers(UserID uid) throws BackendException, RosterItemNotFoundException {
	return backend_.getPublishers(uid);
    }

    @Override
    public RosterItem getRosterItem(UserID rosterOwner, UserID contact) throws RosterItemNotFoundException, BackendException {
	logger_.debug("get the roster item from the backend");
	return backend_.getRosterItem(rosterOwner, contact);
    }

    @Override
    public UserID[] getSubscribers(UserID user) throws RosterItemNotFoundException, BackendException {
	return backend_.getSubscribers(user);
    }

    @Override
    public boolean isASubscriber(UserID ownerID, UserID jid)
	    throws BackendException {
	boolean ret ;
	try {
	    RosterItem item = backend_.getRosterItem(ownerID, jid);
	    if (item.getJID() != null) {
		ret = true;
	    } else {
		ret = false;
	    }
	} catch (RosterItemNotFoundException e) {
	    logger_.debug("the item is not found in "+ ownerID.getID() + " roster");
	    ret = false;
	} catch (BackendException e) {
	    throw new BackendException("un expected error");
	}
	return ret;
    }

}
